package umputun.jutw.utils.common;

/*
 Copyright 2009 Umputun

 This file is part of JUTW.

 JUTW is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 JUTW is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with JUTW.  If not, see http://www.gnu.org/licenses/.
*/

import java.util.*;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class UOpts {
	public UOpts(String[] args) {
		if (args.length == 1 && !args[0].startsWith("--"))
			processArgs(makeArgsFromOptFile(args[0]));
		else
			processArgs(args);
	}


	private String[] makeArgsFromOptFile(String opt_location) {
		File fh = new File(opt_location);
		if (!fh.exists()) {
			System.err.println("Opt file not found, " + opt_location);
			return new String[0];
		}
		List<String> res_list = new ArrayList<String>();
		try {
			BufferedReader rdr = new BufferedReader(new FileReader(fh));
			while(true) {
				String line = rdr.readLine();
				if (line == null) break;
				if (line.length() < 2) continue;
				if (line.length() > 0 && line.startsWith("#")) continue;
				res_list.add(line);
			}
			rdr.close();
			return res_list.toArray(new String[res_list.size()]);
		}
		catch (IOException e) {
			System.err.println("Can't read opt file " + opt_location);
		}
		return new String[0];
	}


	private void processArgs(String[] args) {
		for (String pair: args) {
			String kv[] = pair.split("[=]");

			if(kv[0].startsWith("--")) {

				if (kv[0].equals("--opts")) {
					processArgs(makeArgsFromOptFile(kv[1]));
					continue;
				}

				if (kv.length == 2 ) {
					String key = kv[0].substring(2, kv[0].length());
					String value = pair.substring(kv[0].length()+1);
					if (params.containsKey(key))
						key = key + "#" + params.size();

					params.put(key, value);
				}
				else if(kv.length == 1){
					String key = kv[0].substring(2, kv[0].length());
					params.put(key, BOOLEAN_KEY_DEFINED);
				}
			}
			else {
				params.put(String.valueOf(noname_num++), pair);
			}
		}
	}

	public String get(String key) {
		return  params.containsKey(key) ? params.get(key) : null;
	}

	public List<String> getList(String key) {
		List<String> result = Collections.emptyList();
		if (params.containsKey(key)) {
			result = new ArrayList<String>();
			result.add(params.get(key));
		}
		else
			return result;

		for (int i=1; i<MAX_ELEMS_IN_LIST; i++) {
			String next_key = key + "#" + i;
			if (!params.containsKey(next_key))
				break;
			result.add(params.get(next_key));
		}
		return result;
	}

	public String get(String key, String def) {
		return  params.containsKey(key) ? params.get(key) : def;
	}
	public boolean get(String key, boolean def) {
		if (!params.containsKey(key)) return def;
		String svalue = params.get(key).toLowerCase();
		return (svalue.equals("true") || svalue.equals("yes"));
	}
	public int get(String key, int def) {
		return  params.containsKey(key) ? Integer.valueOf(params.get(key)): def;
	}
	public boolean hasKey(String key) {
		return  params.containsKey(key) ;
	}
	public int getNonameSize() {
		return noname_num;
	}

	public String get(int noname_num) {
		return  params.containsKey(String.valueOf(noname_num)) ? params.get(String.valueOf(noname_num)) : null;
	}

	public Map<String, String> getParams() {
		return Collections.unmodifiableMap(params);
	}

	public static final String BOOLEAN_KEY_DEFINED="true";

	private final Map<String,String> params =  new HashMap<String,String>();
	private int noname_num = 0;
	private static final int MAX_ELEMS_IN_LIST = 64;
}
